<template>
  <view class="container">
    <view class="intro">
      <image src="/static/images/logo.png"></image>
      <view class="tips">
        一杯好茶，一口软欧包
        <br />
        在茶坊小舍遇见两种美好
      </view>
    </view>
    <view class="bottom">
      <!-- #ifdef MP-WEIXIN -->
      <button
        type="primary"
        size="default"
        class="login-btn"
        @tap="getUserProfile"
        lang="zh_CN"
      >
        <image src="/static/images/mine/wechat.png"></image>
        微信一键登录
      </button>
      <!-- #endif -->
     </view>
  </view>
</template>

<script>
// import Member from "@/api/member";
import {mapMutations } from "vuex";

export default {
  data() {
    return {
      code: null,
    };
  },
  onload() {},
  methods: {
    ...mapMutations(["SET_MEMBER"]),
    ...mapMutations(["SET_OPENID"]),
    
    async getUserProfile() {
      this.login();
      wx.getUserProfile({
        desc: "获取用户信息",
        success: (res) => {
          this.SET_MEMBER(res.userInfo);
          uni.navigateBack();
          uni.setStorageSync("userInfo",res.userInfo)
          this.getOpenId(res.userInfo);
		  
        },
      });
    },
    login() {
      wx.login({
        provider: "weixin",
        success: (resCode) => {
          this.code = resCode.code;
        },
      });
    },
    getOpenId(userInfo) {
      let opt = {
        url: "/user/getWeChartUser",
        method: "get",
      };
      let param = {
        code: this.code,
        name:userInfo.nickName,
        headerImg:userInfo.avatarUrl
      };
      this.$req.httpRequest(opt, param).then((res) => {
		
		uni.setStorageSync("token",res.data.data.token)  
		uni.setStorageSync("tokenKey",res.data.data.userInfo.userOpenid)
        this.SET_OPENID(res.data.data.userInfo.userOpenid);
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.intro {
  width: 100%;
  height: 60vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-evenly;
  font-size: $font-size-base;
  color: $text-color-assist;

  image {
    width: 165rpx;
    height: 165rpx;
  }

  .tips {
    line-height: 72rpx;
    text-align: center;
  }
}

.bottom {
  height: 40vh;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 0 40rpx;

  .login-btn {
    width: 100%;
    border-radius: 50rem !important;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 10rpx 0;

    image {
      width: 36rpx;
      height: 30rpx;
      margin-right: 10rpx;
    }
  }

  .row {
    .grid {
      width: 20%;
      image {
        width: 60rpx;
        height: 60rpx;
        margin-bottom: 10rpx;
      }
    }
  }
}
</style>
